"""
根据已建数据库生成模型文件
    sqlacodegen mysql+pymysql://user:password@host/database > models.py
    * 注意: 带特殊字符 如 `@` 需要以 urllib.parse.quote 进行编码 否则将出错
"""

from sqlalchemy import Column, DateTime, String, Text, text, ForeignKey
from sqlalchemy.dialects.mysql import INTEGER, TINYINT
from sql_app.database import Base


class TActivity(Base):  # 活动
    __tablename__ = 't_activity'

    id = Column(INTEGER(11), primary_key=True)
    name = Column(String(255), nullable=False, server_default=text("''"))
    start_time = Column(DateTime, nullable=False)
    end_time = Column(DateTime, nullable=False)
    img_path = Column(Text)
    desc = Column(String(255), nullable=False, server_default=text("''"))
    related_items = Column(Text, comment='活动类型\\r\\n范例:\\r\\n    [\\r\\n      {\\r\\n        “id”: [1, 2, 3],\\r\\n       “type”: “questionnaire”,\\r\\n      },\\r\\n      {\\r\\n       “id”: [4, 5, 6],\\r\\n       “type”: “vote”,\\r\\n      },\\r\\n      {\\r\\n        “id”: [7, 8, 9],\\r\\n       “type”: “lottery”,\\r\\n      }\\r\\n    ]\\r\\n* 这种方式保证活动类型的扩展性；增加一个新的活动只用增加新的 键值对即可\\r\\n')
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))


class TActivityLottery(Base):  # 活动-抽奖
    __tablename__ = 't_activity_lottery'

    id = Column(INTEGER(11), primary_key=True)
    name = Column(String(255), nullable=False, server_default=text("''"))
    type = Column(INTEGER(11), nullable=False, server_default=text("'0'"))
    rules = Column(Text, comment='抽奖策略规则  对象\\r\\n\\r\\n{\\r\\n      "max_draw_times": 2,\\r\\n      "person_day_draw_times": 3,\\r\\n      "point": 20,\\r\\n      "person_max_win_times": 5,\\r\\n      "day_max_win_times": 5,\\r\\n      "draw_mode": 1,\\r\\n      "total_win_rate": "20%",\\r\\n      "goods_provide_interval": 0\\r\\n    }')
    start_time = Column(DateTime)
    end_time = Column(DateTime)
    create_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))


class TActivityLotteryGood(Base):  # 活动-抽奖-奖品
    __tablename__ = 't_activity_lottery_goods'

    id = Column(INTEGER(11), primary_key=True)
    name = Column(String(255), nullable=False, server_default=text("''"))
    type = Column(INTEGER(11), nullable=False)
    subtype = Column(INTEGER(11), nullable=False)
    img_path = Column(Text, nullable=False)
    price = Column(String(255), nullable=False, server_default=text("''"))
    desc = Column(String(255), nullable=False, server_default=text("''"))
    create_time = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))


class TActivityLotteryGoodsType(Base):  # 活动-抽奖-奖品-类型
    __tablename__ = 't_activity_lottery_goods_type'

    id = Column(INTEGER(11), primary_key=True)
    name = Column(String(255), nullable=False, server_default=text("''"))


class TActivityLotteryStatistic(Base):  # 活动-抽奖-结果
    __tablename__ = 't_activity_lottery_statistics'

    id = Column(INTEGER(11), primary_key=True)
    user_id = Column(String(64), nullable=False, server_default=text("''"))
    phone = Column(INTEGER(11), nullable=False)
    win_goods = Column(Text, nullable=False, comment='奖品id数组')
    win_time = Column(DateTime, nullable=False, comment='中奖时间')
    cash_deadline = Column(DateTime, nullable=False, comment='兑奖截至时间')
    cash_time = Column(DateTime, nullable=False, comment='兑奖时间')
    status = Column(TINYINT(4), comment='兑奖状态')


class TActivityQuestionnaire(Base):  # 活动-问卷
    __tablename__ = 't_activity_questionnaire'

    id = Column(INTEGER(11), primary_key=True)
    name = Column(String(255), nullable=False, server_default=text("''"))
    areacode = Column(String(255), nullable=False, server_default=text("''"), comment='区域')
    start_time = Column(DateTime)
    end_time = Column(DateTime, nullable=False)
    create_time = Column(DateTime)
    answer_time = Column(INTEGER(11), comment='答题时长')
    desc = Column(String(255), nullable=False, server_default=text("''"))
    img_path = Column(Text, nullable=False)


class TActivityQuestionnaireQuestion(Base):  # 活动-问卷-问题
    __tablename__ = 't_activity_questionnaire_question'

    id = Column(INTEGER(11), primary_key=True)
    title = Column(String(255), nullable=False, server_default=text("''"))
    questionnaire_id = Column(INTEGER(11), nullable=False, server_default=text("'0'"))
    option_type = Column(INTEGER(11), nullable=False, server_default=text("'0'"), comment='类型')
    options = Column(Text, nullable=False, comment='答案  对象数组\\r\\n\\r\\n范例\\r\\n[\\r\\n    {\\r\\n      "text": "A.满意",\\r\\n      "is_correct": 1,\\r\\n      "score": 2\\r\\n    },\\r\\n    {\\r\\n      "text": "B.不满意",\\r\\n      "is_correct": 0,\\r\\n      "score": 0\\r\\n    }\\r\\n  ]')
    answer_time = Column(INTEGER(11), nullable=False, comment='答题时长')
    release_status = Column(TINYINT(4), nullable=False)
    img_path = Column(Text, nullable=False)
    video_source = Column(INTEGER(11), nullable=False)
    video_url = Column(String(255), nullable=False, server_default=text("''"))
    desc = Column(String(255), nullable=False, server_default=text("''"))
    start_time = Column(DateTime, nullable=False)
    end_time = Column(DateTime, nullable=False)


class TActivityQuestionnaireStatistic(Base):  # 活动-问卷-结果
    __tablename__ = 't_activity_questionnaire_statistics'

    id = Column(INTEGER(11), primary_key=True)
    user_id = Column(String(64), nullable=False, server_default=text("''"))
    question_id = Column(INTEGER(11), nullable=False)
    solution = Column(String(255), nullable=False, server_default=text("''"))
    submit_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))


class TActivityVote(Base):  # 活动-投票
    __tablename__ = 't_activity_vote'

    id = Column(INTEGER(11), primary_key=True)
    name = Column(String(255), nullable=False, server_default=text("''"))
    start_time = Column(DateTime, nullable=False)
    end_time = Column(DateTime, nullable=False)
    areacode = Column(String(255), nullable=False, server_default=text("''"))
    rules = Column(Text, nullable=False, comment='投票规则\\r\\n\\r\\n范例:\\r\\n  [\\r\\n    {\\r\\n      vote_type: 1,\\r\\n      vote_limit: 1\\r\\n    }\\r\\n  ] ')
    img_path = Column(Text, nullable=False)
    video_url = Column(String(255), nullable=False, server_default=text("''"))
    desc = Column(String(255), nullable=False, server_default=text("''"))
    template_id = Column(INTEGER(11))


class TActivityVoteParticipant(Base):  # 活动-投票-投票项
    __tablename__ = 't_activity_vote_participant'

    id = Column(INTEGER(11), primary_key=True)
    name = Column(String(255), nullable=False, server_default=text("''"))
    vote_id = Column(
        INTEGER(11),
        nullable=False,
        server_default=text("'0'"))
    vote_got = Column(INTEGER(11), nullable=False, server_default=text("'0'"), comment='当前票数')
    desc = Column(String(255), nullable=False, server_default=text("''"))
    image_path = Column(Text)
    video_url = Column(String(255), nullable=False, server_default=text("''"))
    start_time = Column(DateTime, nullable=False)
    end_time = Column(DateTime, nullable=False)


class TActivityVoteStatistic(Base):  # 活动-投票-结果
    __tablename__ = 't_activity_vote_statistics'

    id = Column(INTEGER(11), primary_key=True)
    vote_id = Column(INTEGER(11))
    user_id = Column(String(64), nullable=False, server_default=text("''"))
    participant_id = Column(INTEGER(11), nullable=False, comment='投票项id')
    vote_num = Column(INTEGER(11), nullable=False, server_default=text("'0'"))
    submit_time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))

